package com.nameli.admin.controller;

import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.dto.ConfirmCheckDTO;
import com.nameli.SIM.bean.entity.MajorInfo;
import com.nameli.SIM.bean.query.MajorInfoQuery;
import com.nameli.SIM.bean.vo.MajorInfoVO;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.SIM.util.EmptyUtil;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.admin.service.CheckMajorInfoService;
import com.nameli.admin.service.MajorService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author NAME-L
 * @Description TODO
 * @className StudentController
 * @date 2021-10-31 16:31:48
 */
@RestController
@CrossOrigin
@RequestMapping("/major")
public class MajorController {
    private static final Log log = LogFactory.getLog(MajorController.class);

    @Autowired
    private MajorService majorService;
    @Autowired
    private CheckMajorInfoService checkMajorInfoService;

    /**
     * 查询所有专业信息
     *
     * @param pageIndex
     * @param pageSize
     * @return MajorInfoVO
     */
    @GetMapping("/index")
    @RequiresPermissions("department:major:list")
    public JsonResult queryAllByPage(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize) {
        JsonResult jsonResult = null;
        log.info("enter into -----------> MajorController - queryAllByPage");
        if (EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)) {
            log.info("pageIndex:" + pageIndex + "\t" + "pageSize:" + pageSize);
            log.info("check -----------> pageIndex & pageSize 不为空");
            PageInfo<MajorInfoVO> returnList = majorService.queryAllMajor(pageIndex, pageSize);
            jsonResult = JsonResult.querySuccessFul(returnList);
        } else {
            log.info("check -----------> pageIndex & pageSize 为空");
            jsonResult = JsonResult.doFailed();
        }
        return jsonResult;
    }

    /**
     * 根据条件查找信息
     *
     * @return
     */
    @GetMapping("/info/condition")
    @RequiresPermissions("department:major:list")
    public JsonResult queryByCondition(MajorInfoQuery majorInfoQuery, @RequestParam(value = "pageIndex") int pageIndex, @RequestParam(value = "pageSize") int pageSize) {
        log.info("enter into -----------> MajorController - queryByCondition");
        JsonResult jsonResult = null;
        if (EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)) {
            log.info("pageIndex:" + pageIndex + "\t" + "pageSize:" + pageSize);
            log.info("check -----------> pageIndex & pageSize 不为空");
            if (EmptyUtil.isNotEmpty(majorInfoQuery)) {
                log.info("查询条件不为空");
                log.info("output -----------> majorInfoQuery:" + majorInfoQuery);
                PageInfo<MajorInfoVO> returnList = majorService.queryByCondition(majorInfoQuery, pageIndex, pageSize);
                jsonResult = JsonResult.querySuccessFul(returnList);
            } else {
                jsonResult = JsonResult.doFailed();
            }
        }
        return jsonResult;
    }

    /**
     * 专业信息修改
     *
     * @param majorInfo
     * @return
     */
    @PostMapping("/update/info")
    public JsonResult updateMajor(MajorInfo majorInfo) {
        log.info("enter into -----------> MajorController - updateMajor");
        log.info("要修改的实体类:" + majorInfo);
        JsonResult jsonResult = null;
        try {
            Boolean flag = majorService.updateMajorInfo(majorInfo);
            log.info("flag :" + flag);
            if (flag) {
                jsonResult = JsonResult.updateSuccessFul();
            } else {
                jsonResult = JsonResult.doFailed();
            }
        } catch (Exception e) {
            log.info("更新专业信息异常:" + e);
            jsonResult = JsonResult.systemError();
        }
        return jsonResult;
    }

    /**
     * 添加专业信息
     *
     * @param majorInfo
     * @return
     */
    @PostMapping("/insert/info")
    public JsonResult insertMajor(@RequestBody MajorInfo majorInfo) {
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        Map<String, Object> returnMap = new HashMap<>();
        log.info("enter into -----------> MajorController - updateMajor");
        log.info("新增专业信息为：" + majorInfo);
        try {
            Boolean flag = majorService.insertMajor(majorInfo);
            returnMap.put("flag", flag);
            if (flag) {
                errorCode = ReturnStatus.CREATED;
                errorMsg = "添加成功";
                jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnMap);

            } else {
                errorCode = ReturnStatus.DO_FAILED;
                errorMsg = "添加失败";
                jsonResult = JsonResult.returnMethod(errorCode, errorMsg, returnMap);
            }
        } catch (Exception e) {
            log.info("添加专业信息异常:" + e);
            errorCode = ReturnStatus.ERROR;
            errorMsg = "系统异常";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }

    /**
     * 确认通知
     * @param
     * @return
     */
    @PutMapping("/check")
//    public JsonResult confirmCheck(@PathVariable(value = "majorNo") String majorNo, @PathVariable(value = "status") String status) {
    public JsonResult confirmCheck(@RequestBody ConfirmCheckDTO checkDTO) {
        String majorNo = checkDTO.getMajorNo();
        String status = checkDTO.getStatus();
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        log.info("确认的majorNo:" + majorNo);
        log.info("status:" + status);
        Boolean flag = majorService.updateConfirmCheck(majorNo, status);
        if (flag) {
            errorCode = ReturnStatus.SUCCESS;
            errorMsg = "确认成功";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        } else {
            errorCode = ReturnStatus.DO_FAILED;
            errorMsg = "确认失败";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        }
        return jsonResult;
    }
}
